home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / src / srgp_canvas.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-09  |  6.6 KB  |  267 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3.  
  4. #ifdef THINK_C
  5. #include "ChooseWhichQuickDraw.h"
  6. #endif
  7.  
  8. /** ABOUT CANVASES
  9. At any given time, there is one ACTIVE canvas.
  10.  
  11. The ACTIVE canvas is the one affected by all output primitives.
  12.  
  13. Initially, canvas #0 is the only canvas; it is always visible and
  14.    is the size of the screen.  Its coordinate system is the
  15.    one involved in all input commands.
  16.  
  17. The permanent spec for each canvas is stored in a
  18.    table (srgp__canvasTable).
  19. But the spec for the currently active canvas is
  20.    stored in a cache (srgp__curActiveCanvasSpec) and sometimes
  21.    is more current than the permanent version.
  22. **/
  23.    
  24.  
  25.  
  26.  
  27. /** INTERNAL: setting canvas defaults
  28. Sets the defaults for the currently active canvas.
  29. Affects the cache (srgp__curActiveCanvasSpec) first.
  30. Then copies into the permanent table.
  31. Assumes that "max_coord" fields are already set.
  32. **/
  33.  
  34. void
  35. SRGP__setCanvasDefaults ()
  36. {
  37.    PUSH_TRACE;
  38.  
  39.    /* Fool the attribute routines so they *have* to do the work. */
  40.    memset (&srgp__curActiveCanvasSpec.attributes, -1, 
  41.        sizeof(srgp__attribute_group));
  42.  
  43.    SRGP_setWriteMode (WRITE_REPLACE);
  44.    SRGP_setClipRectangle
  45.       (SRGP_defRectangle
  46.      (0,0,
  47.       srgp__curActiveCanvasSpec.max_xcoord,
  48.       srgp__curActiveCanvasSpec.max_ycoord));
  49.  
  50.    SRGP_setFont (0);
  51.    SRGP_setLineStyle (CONTINUOUS);
  52.    SRGP_setLineWidth (1);
  53.    SRGP_setMarkerStyle (MARKER_CIRCLE);
  54.    SRGP_setMarkerSize (10);
  55.    SRGP_setFillStyle (SOLID);
  56.    SRGP_setPenStyle (SOLID);
  57.    SRGP_setFillBitmapPattern (0);
  58.    SRGP_setFillPixmapPattern (0);
  59.    SRGP_setPenBitmapPattern (0);
  60.    SRGP_setPenPixmapPattern (0);
  61.  
  62.    /* ERASE CANVAS MANUALLY. */
  63.    SRGP_setColor (0);
  64.    SRGP_fillRectangleCoord
  65.       (0,0,
  66.        srgp__curActiveCanvasSpec.max_xcoord,
  67.        srgp__curActiveCanvasSpec.max_ycoord);
  68.    SRGP_setColor (1);
  69.    SRGP_setBackgroundColor (0);
  70.  
  71.    /* MAKE SURE CHANGES GET INTO THE ACTUAL TABLE (not just in the cache) */
  72.    srgp__canvasTable[srgp__curActiveCanvasId] = srgp__curActiveCanvasSpec;
  73.  
  74.    POP_TRACE;
  75. }
  76.  
  77.  
  78.  
  79.  
  80. /*!*/
  81. void
  82. SRGP_useCanvas (canvasID canvas_id)
  83. {
  84.    DEBUG_AIDS{
  85.       SRGP_trace (SRGP_logStream, "SRGP_useCanvas: %d\n", canvas_id);
  86.       srgp_check_system_state();
  87.       srgp_check_extant_canvas(canvas_id);
  88.       LeaveIfNonFatalErr();
  89.    }
  90.  
  91.    /* UPDATE PERMANENT COPY OF SPEC FOR CURRENT ACTIVE CANVAS. */
  92.    srgp__canvasTable[srgp__curActiveCanvasId] = srgp__curActiveCanvasSpec;
  93.  
  94.    /* SET NEW ACTIVE, and LOAD CACHE. */
  95.    srgp__curActiveCanvasId = canvas_id;
  96.    srgp__curActiveCanvasSpec = srgp__canvasTable[srgp__curActiveCanvasId];
  97.  
  98. #ifdef THINK_C
  99.    SetPort (srgp__canvasTable[srgp__curActiveCanvasId].drawable.win);
  100. #endif
  101. }
  102.  
  103.  
  104.  
  105.  
  106. /*!*/
  107. canvasID
  108. SRGP_createCanvas (int width, int height)
  109. {
  110.    register int new_canvas_id;
  111.    boolean free_one_found;
  112.    canvas_spec *canvsptr;
  113.  
  114.    DEBUG_AIDS{
  115.       SRGP_trace (SRGP_logStream, "SRGP_createCanvas: %d %d\n", width, height);
  116.       srgp_check_system_state();
  117.       LeaveIfNonFatalErr();
  118.    }
  119.  
  120.    /* FIND AN EMPTY ENTRY IN CANVAS TABLE. */
  121.    new_canvas_id = 1;
  122.    free_one_found = FALSE;
  123.    while ((new_canvas_id <= MAX_CANVAS_INDEX) && !free_one_found)
  124.       if (srgp__canvasTable[new_canvas_id].drawable.bitmap != 0) 
  125.          new_canvas_id++;
  126.       else
  127.      free_one_found = TRUE;
  128.  
  129.    if (!free_one_found) {
  130.       SRGP__error (ERR_CANVAS_TABLE_FULL);
  131.       return (-1);
  132.    }
  133.  
  134.    /* ALL SYSTEMS ARE GO! */
  135.    canvsptr = &(srgp__canvasTable[new_canvas_id]);
  136.  
  137.    /* ALLOCATE THE BITMAP */
  138. #ifdef X11
  139.    if ( (canvsptr->drawable.bitmap =
  140.      XCreatePixmap (srgpx__display,
  141.             srgp__canvasTable[0].drawable.win,
  142.             width, height,
  143.             srgp__available_depth)) 
  144.                                         == 0) {
  145.       SRGP__error (ERR_MALLOC);
  146.       return (-1);
  147.    }
  148. #endif
  149.  
  150. #ifdef THINK_C
  151. #ifdef COLOR_QUICKDRAW
  152. {
  153.    CGrafPtr cgptr;
  154.    int depth, rowBytes;
  155.    long size;
  156.    Rect bounds;
  157.    Ptr myBits;
  158.    
  159.    SetRect (&bounds, 0, 0, width, height);
  160.    OpenCPort (cgptr = canvsptr->drawable.xid = 
  161.           (CGrafPtr)(NewPtr(sizeof(CGrafPort))));
  162.    depth = (**(*cgptr).portPixMap).pixelSize;
  163.    rowBytes = (((depth * width) + 15) >> 4) << 1;
  164.    size = (long) height * rowBytes;   
  165.    if ((myBits = NewPtr(size)) == NULL) {
  166.       SRGP__error (ERR_MALLOC);
  167.       return (-1);
  168.    }
  169.    (**(*cgptr).portPixMap).baseAddr = myBits;
  170.    (**(*cgptr).portPixMap).rowBytes = rowBytes + 0x8000;
  171.    (**(*cgptr).portPixMap).bounds = bounds;
  172. }
  173.  
  174. #else
  175.  
  176. {
  177.    GrafPtr cgptr;
  178.    int rowBytes;
  179.    long memreq;
  180.    Rect bounds;
  181.    BitMap newBitMap;
  182.    Ptr myBits;
  183.  
  184.    SetRect (&bounds, 0, 0, width, height);
  185.    OpenPort (cgptr = canvsptr->drawable.xid = 
  186.          (GrafPtr)(NewPtr(sizeof(GrafPort))));
  187.    newBitMap.rowBytes = (width>>3);
  188.    if ((width % 8) > 0)
  189.       newBitMap.rowBytes ++;
  190.    if ((rowBytes % 2) > 0) 
  191.       newBitMap.rowBytes++;
  192.    SetRect (&newBitMap.bounds, 0, 0, width, height);
  193.    memreq = newBitMap.rowBytes * height;
  194.    if ((newBitMap.baseAddr = NewPtr(memreq)) == NULL) {
  195.       SRGP__error (ERR_MALLOC);
  196.       return (-1);
  197.    }
  198.    else {
  199.       SetPortBits (&newBitMap);
  200.       thePort->portRect = newBitMap.bounds;
  201.    }
  202. }
  203. #endif
  204. #endif
  205.  
  206.    /* SET VARIOUS IMPORTANT FIELDS IN THE CANVAS SPEC. */
  207.    canvsptr->max_xcoord = width-1;
  208.    canvsptr->max_ycoord = height-1;
  209.  
  210.  
  211. #ifdef X11
  212.    canvsptr->gc_fill =
  213.       XCreateGC (srgpx__display, canvsptr->drawable.bitmap, 0L, NULL);
  214.    canvsptr->gc_frame =
  215.       XCreateGC (srgpx__display, canvsptr->drawable.bitmap, 0L, NULL);
  216. #endif
  217.  
  218.  
  219.    /* TELL THE WORLD TO PREPARE TO OUTPUT TO NEW BITMAP */
  220.    PUSH_TRACE;
  221.    SRGP_useCanvas (new_canvas_id);
  222.    SRGP__setCanvasDefaults();
  223.    POP_TRACE;
  224.  
  225.    return new_canvas_id;
  226. }
  227.  
  228.  
  229.  
  230.  
  231. /*!*/
  232. void
  233. SRGP_deleteCanvas (canvasID canvas_id)
  234. {
  235.    DEBUG_AIDS {
  236.       SRGP_trace (SRGP_logStream, "SRGP_deleteCanvas: %d\n", canvas_id);
  237.       srgp_check_system_state();
  238.       srgp_check_extant_canvas(canvas_id);
  239.       LeaveIfNonFatalErr();
  240.    }
  241.  
  242.    if (canvas_id == SCREEN_CANVAS)
  243.       SRGP__error (ERR_DELETION_OF_SCREEN);
  244.    if (canvas_id == srgp__curActiveCanvasId)
  245.       SRGP__error (ERR_DELETION_OF_ACTIVE_CANVAS);
  246.    LeaveIfNonFatalErr();
  247.  
  248. #ifdef X11
  249.    XFreePixmap (srgpx__display, srgp__canvasTable[canvas_id].drawable.bitmap);
  250. #endif
  251.  
  252. #ifdef THINK_C
  253. #ifdef COLOR_QUICKDRAW
  254. #  define canvasGrafport (*((CGrafPtr)(srgp__canvasTable[canvas_id].drawable.xid)))
  255.    DisposPtr ((**(canvasGrafport.portPixMap)).baseAddr);
  256. #else
  257. #  define canvasGrafport (*((GrafPtr)(srgp__canvasTable[canvas_id].drawable.xid)))
  258.    DisposPtr (canvasGrafport.portBits.baseAddr);
  259. #endif
  260.    ClosePort (srgp__canvasTable[canvas_id].drawable.xid);
  261.    DisposPtr (srgp__canvasTable[canvas_id].drawable.xid);
  262. #endif
  263.  
  264.    srgp__canvasTable[canvas_id].drawable.bitmap = 0; /* FREES THE TABLE ENTRY */
  265. }
  266.  
  267.